package com.xebest.web.controller.storehouse;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.config.ParamConfig;
import com.xebest.constant.XeConstans;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.msg.XeVenStoreHouseMsg;
import com.xebest.web.form.XeVenStoreInvForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenStoreInventoryService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenStoreInvProView;
import com.xebest.web.view.XeVenStoreInventoryView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 仓库监控
 *
 * @author 杜春静
 */
@RestController
@RequestMapping("storeInvtory")
public class XeVenStoreInventoryController extends BaseController {

    private Logger log = LoggerFactory.getLogger(XeVenStoreInventoryController.class);

    @Autowired
    XeVenStoreInventoryService xeVenStoreInventoryService;//仓库一览
    @Autowired
    private ParamConfig paramConfig;//获取公共配置信息


    /**
     * 查询库存监控列表
     *
     * @author 杜春静
     */
    @RequestMapping("queryStoreInvList")
    public BaseResponse queryStoreInvList(@RequestBody XeVenStoreInvForm form) {

        log.info("查询库存监控列表请求参数为：{}", JSON.toJSONString(form));
        if (null == form) {
            form = new XeVenStoreInvForm();
        }
        XePageInfo<XeVenStoreInventoryView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //封装查询参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        paramMap.put("form", form);
        try {
            List<XeVenStoreInventoryView> storeInvList = this.xeVenStoreInventoryService.queryStoreInvList(paramMap);
            xePageInfo = new XePageInfo<XeVenStoreInventoryView>(storeInvList);
            //封装返回数据
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", storeInvList);
            map.put("xePageInfo", xePageInfo);
            String picServer = paramConfig.getImgUrl() + XeConstans.ERP_PRO_PIC_DIR;//图片服务器地址
            map.put("picServer", picServer);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("--------------查询库存监控列表 异常信息为：{}", e);
            return sendAjaxError();
        }
    }

    /**
     * 修改仓库上下限
     *
     * @author 杜春静
     */
    @RequestMapping("batchEditStoreInvLimit")
    public BaseResponse batchEditStoreInvLimit(@RequestBody XeVenStoreInvForm form, HttpServletRequest request) {
        log.info("修改仓库上下限请求参数为：{}", JSON.toJSONString(form));

        //验证参数
        if (null == form || null == form.getStoreInvIds() || null == form.getInvMax()
                || null == form.getInvMin()) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }

        //判断库存上限是否小于库存下限
        if ((form.getInvMax()).compareTo(form.getInvMin()) <= 0) {

            return sendAjaxError(XeVenStoreHouseMsg.STORE_INV_LIMIT);
        }

        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        //输出结果
        try {
            //封装要插入的库存上下限
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("storeInvIds", form.getStoreInvIds());
            params.put("userCode", userInfo.getUserCode());
            params.put("invMax", form.getInvMax());
            params.put("invMin", form.getInvMin());

            int result = this.xeVenStoreInventoryService.batchUpdateInvLimit(params);
            if (result > 0) {
                return sendAjaxOK();
            }
            return sendAjaxError();

        } catch (Exception e) {
            log.error("修改仓库上下限异常：" + e);
            return sendAjaxError();
        }

    }

    /**
     * 编辑仓库库存上下限
     *
     * @author 杜春静
     */
    @RequestMapping("editStoreInvLimit")
    public BaseResponse editStoreInvLimit(@RequestBody XeVenStoreInvForm form, HttpServletRequest request) {
        log.info("修改仓库上下限请求参数为：{}", JSON.toJSONString(form));
        //1、验证参数
        if (null == form || null == form.getStoreInvId() || StringUtils.isEmpty(form.getStoreInvId())
                || null == form.getInvMax() || null == form.getInvMin()) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        if ((form.getInvMax()).compareTo(form.getInvMin()) <= 0) {

            return sendAjaxError(XeVenStoreHouseMsg.STORE_INV_LIMIT);
        }
        //2、获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        //3、输出结果
        try {
            int result = this.xeVenStoreInventoryService.updateStoreInv(form, userInfo);
            if (result > 0) {
                return sendAjaxOK();
            }
            return sendAjaxError();

        } catch (Exception e) {
            log.error("修改仓库上下限异常：" + e);
            return sendAjaxError();
        }

    }

    /**
     * 查询库存商品列表
     *
     * @author 杜春静
     */
    @RequestMapping("queryInvProList")
    public BaseResponse queryInvProList(@RequestBody XeVenStoreInvForm form, HttpServletRequest request) {
        log.info("查询库存商品列表请求参数为：{}", JSON.toJSONString(form));
        //验证参数
        if (null == form) {

            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        if (!StringUtils.isEmpty(form.getSearchType())) {
            Integer searchType = form.getSearchType();
            switch (searchType) {
                case 2:
                    if (!StringUtils.isEmpty(form.getProName())) {
                        form.setProNum(form.getProName());
                        form.setProName("");
                    }
                    break;
                default:
                    break;
            }
        }
        //设置供应商编码
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());

        XePageInfo<XeVenStoreInvProView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //库存商品列表
        List<XeVenStoreInvProView> resultList = new ArrayList<XeVenStoreInvProView>();
        try {
            resultList = this.xeVenStoreInventoryService.queryInvProList(form);
            for (XeVenStoreInvProView storeInvView : resultList) {
                storeInvView.setCreateUser(userInfo.getLoginName());
            }
            xePageInfo = new XePageInfo<XeVenStoreInvProView>(resultList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", resultList);
            map.put("xePageInfo", xePageInfo);
            String picServer = paramConfig.getImgUrl() + XeConstans.ERP_PRO_PIC_DIR;//图片服务器地址
            map.put("picServer", picServer);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询库存商品列表失败，异常信息为：{}", e);
            return this.sendAjaxError();
        }
    }

}
  
